<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/23 0023
  Time: 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <div id="page_nav">
        <c:if test="${page.pageNum > 1}">
            <a href="${url}pageNum=${page.firstPage}">首页</a>
            <a href="${url}pageNum=${page.prePage}">上一页</a>
        </c:if>

        <%--显示页码开始--%>
        <c:choose>
            <c:when test="${page.pages <= 5}">
                <c:set var="begin" value="1" scope="page"></c:set>
                <c:set var="end" value="${page.pages}" scope="page"></c:set>
            </c:when>
            <c:when test="${page.pages > 5}">
                <c:choose>
                    <c:when test="${page.pageNum <= 3}">
                        <c:set var="begin" value="1" scope="page"></c:set>
                        <c:set var="end" value="5" scope="page"></c:set>
                    </c:when>
                    <c:when test="${page.pages - page.pageNum <= 2}">
                        <c:set var="begin" value="${page.pages - 4}" scope="page"></c:set>
                        <c:set var="end" value="${page.pages}" scope="page"></c:set>
                    </c:when>
                    <c:otherwise>
                        <c:set var="begin" value="${page.pageNum - 2}" scope="page"></c:set>
                        <c:set var="end" value="${page.pageNum + 2}" scope="page"></c:set>
                    </c:otherwise>
                </c:choose>
            </c:when>
        </c:choose>
        <%--显示页码结束--%>
        <c:forEach begin="${begin}" end="${end}" varStatus="i">
            <c:if test="${i.index == page.pageNum}">
                [${i.index}]
            </c:if>
            <c:if test="${i.index != page.pageNum}">
                <a href="${url}pageNum=${i.index}">${i.index}</a>
            </c:if>
        </c:forEach>

        <c:if test="${page.pageNum < page.pages}">
            <a href="${url}pageNum=${page.nextPage}">下一页</a>
            <a href="${url}pageNum=${page.lastPage}">末页</a>
        </c:if>

        共${page.pages}页，${page.total}条记录
        到第<input value="${page.pageNum}" name="pn" id="pn_input"/>页
        <input type="button" id="searchPageBtn" value="确定">
    </div>
    <script type="text/javascript">
        $(function () {
            $("#searchPageBtn").click(function () {
                var searchPage = $("#pn_input").val();//获取用户要去的页码
                if (searchPage < 1){
                    searchPage = 1;
                }
                if (searchPage > ${page.pages}){
                    searchPage = ${page.pages};
                }
                location.href = "${url}pageNum=" + searchPage;
            });
        });
    </script>
</body>
</html>
